package com.mo.ld.activity.view

import android.os.Bundle
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.dylanc.longan.activity
import com.mo.ld.R
import com.mo.ld.base.BBaseListActivity
import com.mo.lib.utils.setClick
import com.mo.lib.utils.showToast
import com.mo.lib.view.recyclerview.KRecyclerView
import com.mo.lib.view.recyclerview.layoutG
import com.mo.lib.view.recyclerview.setAdapterQuick
import com.mo.lib.view.recyclerview.setItemToTop

/**
 * @ author：mo
 * @ data：2022/3/28:17:33
 * @ 功能：
 */
class RecyclerViewActivity : BBaseListActivity<String>() {
    override fun creatAdaper(): BaseQuickAdapter<String, BaseViewHolder> {
        return object : BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_main, null) {
            override fun convert(holder: BaseViewHolder, item: String) {
                holder.getView<TextView>(R.id.tv_item_main).text = item
            }
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        val krv = KRecyclerView(this)
        val header = TextView(activity)
        header.text = "头"
        val foot = TextView(activity)
        foot.text = "尾"
        krv.layoutG(4).setAdapterQuick(R.layout.item_main, arrayListOf<String>("可下拉", "空数据", "加头", "减头", "加尾", "减尾"), block = { holder, item ->
            holder.getView<TextView>(R.id.tv_item_main).text = item
            holder.itemView.setClick {
                when (holder.layoutPosition) {
                    0 -> {
                        viewBinding?.ptrlBaseList?.setCanRefresh(true)
                        viewBinding?.ptrlBaseList?.setCanLoadMore(true)
                        getList(1)
                    }
                    1 -> {
                        loadList(null)
                    }
                    2 -> {
                        val num = adapter?.setHeaderView(header)
                        viewBinding?.krvBaseList?.setItemToTop(num!!)
                    }
                    3 -> {
                        adapter?.removeHeaderView(header)
                    }
                    4 -> {
                        val num = adapter?.setFooterView(foot)
                        handler.post {
                            viewBinding?.krvBaseList?.scrollToPosition(adapter?.data?.size!!)
                        }
                    }
                    5 -> {
                        adapter?.removeFooterView(foot)
                    }
                }
                showToast(item)
            }
        })
        viewBinding?.llBaseListBottom?.addView(krv)
        loadList(null)
    }

    override fun getList(page: Int) {
        handler.post {
            loadList(arrayListOf("1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1"))
        }
    }

    override fun getMore(page: Int) {
        handler.post {
            loardMore(arrayListOf("2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2"))
        }
    }


}